#!/bin/bash

echo "安装所需依赖包..."

sudo apt-get upgrade
sudo apt-get install python3-pip
pip3 install pyserial
pip3 install Cython
pip3 install setuptools
pip3 install wheel
pip3 install PyYAML==6.0
sudo -H pip3 install rospkg

countdown=5

echo "等待 $countdown 秒以确保串口设备释放或连接成功" 

# 查找所有 /dev/ttyACM* 设备路径

for ((i=$countdown; i>-1; i--));do
    DEVICE_PATHS=$(sudo ls /dev/ttyACM*)
    #echo "找到 $DEVICE_PATH 串口设备"
    echo "$i"
    sleep 1
done


# 检查是否找到了设备路径
if [ -z "$DEVICE_PATHS" ]; then
    echo "没有找到关于 /dev/ttyACM* 的设备，请检查CAN模块设备是否连接成功。"
    exit 1
fi

echo "找到/dev/ttyACM*串口设备"

# 设置权限
for DEVICE_PATH in $DEVICE_PATHS; do
    sudo chmod +x **/*.py
    echo "设置 $DEVICE_PATH 串口设备权限"
    sudo chmod 666 "$DEVICE_PATH"
done

# 启动 ROS 节点
roslaunch DrRobotController_six_axes_can DrRobotController_six_axes.launch device_paths:="$DEVICE_PATHS"
